<?php

/**
 * Created by JetBrains PhpStorm.
 * User: taoqili
 * Date: 12-7-18
 * Time: 上午10:42
 */
header("Content-Type: text/html; charset=utf-8");
error_reporting(E_ERROR | E_WARNING);
date_default_timezone_set("Asia/chongqing");
include "Uploader.class.php";
//上传图片框中的描述表单名称，
$title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
$path = htmlspecialchars($_POST['dir'], ENT_QUOTES);
$globalConfig = include ("config.php");
$imgSavePathConfig = $globalConfig['imageSavePath'];

//获取存储目录
if (isset($_GET['fetch'])) {
    
    header('Content-Type: text/javascript');
    echo 'updateSavePath(' . json_encode($imgSavePathConfig) . ');';
    return;

}

//上传配置
$config = array(
        "savePath" => $imgSavePathConfig,
        "maxSize" => 1000, //单位KB
        "allowFiles" => array(
                ".gif",
                ".png",
                ".jpg",
                ".jpeg",
                ".bmp"),
        "fileNameFormat" => $_POST['fileNameFormat']);

if (empty($path)) {
    
    $path = $config['savePath'][0];

}

//上传目录验证
if (! in_array($path, $config['savePath'])) {
    //非法上传目录
    echo '{"state":"\u975e\u6cd5\u4e0a\u4f20\u76ee\u5f55"}';
    return;
}

$config['savePath'] = $path . '/';

//生成上传实例对象并完成上传
$up = new Uploader("upfile", $config);

/**
 * 得到上传文件所对应的各个参数,数组结构
 * array(
 * "originalName" => "",   //原始文件名
 * "name" => "",           //新文件名
 * "url" => "",            //返回的地址
 * "size" => "",           //文件大小
 * "type" => "" ,          //文件类型
 * "state" => ""           //上传状态，上传成功时必须返回"SUCCESS"
 * )
 */
$info = $up->getFileInfo();
/*
$imageconfig = include_once '../../../../config/base.php';
if (empty($imageconfig['thumb']['width']) || empty($imageconfig['thumb']['height'])) {
    echo 'please set thumbs width and height in base.php ';
    exit();
}
$savePath = $config['savePath'] . date("Ym", time()) . DIRECTORY_SEPARATOR;

//修正从服务器图片中选择图片 不再进行缩略图
//water($info['url'], $imageconfig['thumb']['width'], $imageconfig['thumb']['height'], $savePath);
/**
 * 向浏览器返回数据json数据
 * {
 * 'url'      :'a.jpg',   //保存后的文件路径
 * 'title'    :'hello',   //文件描述，对图片来说在前端会添加到title属性上
 * 'original' :'b.jpg',   //原始文件名
 * 'state'    :'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
 * }
 */
//修改它的返回值

$pathArray = explode("../../../", $info['url']);
$info['url'] = "/static/" . $pathArray[1];

echo "{'url':'" . $info["url"] . "','title':'" . $title . "','original':'" . $info["originalName"] . "','state':'" . $info["state"] . "'}";

/**
 *
 * 缩略图加水印
 * @param unknown_type $id
 */
function water($imagefile, $thumb_maxwidth, $thumb_maxheight, $save_path)
{
    //配置参数
    $config = array();
    //图片水印
    $config["watermark"]["type"] = 0; //是否加水印：0不加水印，1加文字水印，2加图片水印
    $config["watermark"]["text"] = ""; //水印文字内容
    $config["watermark"]["fontfile"] = getcwd() . "/static/public/font/msyh.ttf"; //字体文件
    $config["watermark"]["fontsize"] = 14; //字体大小
    $config["watermark"]["minwidth"] = 300; //图片最小宽度：大于此值时加水印
    $config["watermark"]["minheight"] = 200; //图片最小高度：大于此值时加水印
    $config["watermark"]["logo"] = getcwd() . "/static/news/thumbs/water.png"; //水印LOGO地址
    $config["watermark"]["transparent"] = 80; //水印LOGO不透明度
    //增加检测字体是否存在
    if (! file_exists($config['watermark']['fontfile']) && $config['watermark']['type'] == 1) {
        echo '字体' . $config['watermark']['fontfile'] . '不存在，请检查';
        return null;
    }
    if (! file_exists($config['watermark']['logo']) && $config['watermark']['type'] == 2) {
        echo '水印' . $config['watermark']['logo'] . '不存在，请检查';
        return null;
    }
    //生成缩略图
    //   $imagefile = getcwd() . $imagefile;
    $imagefile_s = $save_path . 'small_' . basename($imagefile);
    require '../../../../lib/Imagetranscls.php';
    $imagetrans = new Imagetranscls();
    $imagetrans->originimage = $imagefile;
    $imagetrans->thumbimage = $imagefile_s;
    $imagetrans->thumb_maxwidth = $thumb_maxwidth;
    $imagetrans->thumb_maxheight = $thumb_maxheight;
    $isokid = $imagetrans->gen_thumbimage();
    //$imagetrans = null;
    if ($config["watermark"]["type"] == 2) {
        //LOGO水印
        $imagetrans->originimage = $imagefile;
        $imagetrans->watermark_minwidth = $config["watermark"]["minwidth"];
        $imagetrans->watermark_minheight = $config["watermark"]["minheight"];
        $imagetrans->watermark_logo = $config["watermark"]["logo"];
        $imagetrans->watermark_transparent = $config["watermark"]["transparent"];
        $isokid = $imagetrans->add_watermark2();
        $imagetrans = null;
    } else {
        //文字水印
        $imagetrans->originimage = $imagefile;
        $imagetrans->watermark_minwidth = $config["watermark"]["minwidth"];
        $imagetrans->watermark_minheight = $config["watermark"]["minheight"];
        $imagetrans->watermark_text = $config["watermark"]["text"];
        $imagetrans->watermark_fontfile = $config["watermark"]["fontfile"];
        $imagetrans->watermark_fontsize = $config["watermark"]["fontsize"];
        $isokid = $imagetrans->add_watermark1();
        $imagetrans = null;
    }

     // return "/static/upload/thumbs/" . basename($imagefile);
}
